package com.ymatou.iapi.livebuyer.testcase;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livebuyer.parameter.SecKillProductStockChangeBean;
import com.ymatou.iapi.livebuyer.service.SecKillProductStockChangeCall;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.utils.RemoteShellExecutor2;
import com.ymttest.utils.TestDataManager;
/**
*直播秒杀商品活动库存变更
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SecKillProductStockChange{
	private static SecKillProductStockChangeBean seckillproductstockchangeBean;
	private static SecKillProductStockChangeCall seckillproductstockchangeCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("直播秒杀商品活动库存变更");
	}

	@Before
	public void caseUp() {
		seckillproductstockchangeBean=new SecKillProductStockChangeBean();
		seckillproductstockchangeCall=new SecKillProductStockChangeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(seckillproductstockchangeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	@Deprecated
	//接口删除
	public void Tc_SecKillProductStockChange_001() {
		Logger.start(true,"直播秒杀商品活动库存变更成功");
		try {
			ExecutorService executor = Executors.newFixedThreadPool(1);    
			List<Userinfo> list =new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerProductRequest( uinfo.getUserId());
			String pid = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			
			Logger.comment("创建商品：" + pid);
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(uinfo);
			//测试时创建真实group
//			clive.getLiveDto().setGroupId(LiveRoomCallService.CreateGroup("AG"+TestDataManager.getRandomNum(5)));
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			
			RemoteShellExecutor2 r2=new RemoteShellExecutor2("172.16.101.117","ftpuser","1qaz@WSX");
			r2.login();
			InputStream stream = r2.getLog("tail -f /usr/local/log/livebuyer.iapi.ymatou.com/default.log");
			BufferedReader br = new BufferedReader(new InputStreamReader(stream));
			ArrayList<String> logs=new ArrayList<String>();
			Runnable r1=new Runnable() {
				public void run() {
					while (true) {
						String line=new String();
						try {
							line =br.readLine();
						} catch (IOException e) {
							e.printStackTrace();
						}
						if(null==line){
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}else{
							logs.add(line);
						}
					}
				}
			};
			executor.submit(r1);
			seckillproductstockchangeBean.setProductId(pid);
			seckillproductstockchangeCall.setData(seckillproductstockchangeBean);
			seckillproductstockchangeCall.callService();			
			Logger.verifyEquals("ok", seckillproductstockchangeCall.getReturnData(), "验证返回");
			//验证日志 SecKillProductStockChangeReq  {"Content":"{\"BizType\":101}","GroupId":"XXXX"}
			Thread.sleep(3000);
			boolean haslog=false;
			for(int i=0;i<logs.size();i++){
				String log=logs.get(i);
				if(log.toString().contains("\\\"BizType\\\":101")){
					haslog=true;
				}
			};
			Logger.verifyEquals(true, haslog, "验证含有log-BizType101");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	@Ignore
	public void Tc_SecKillProductStockChange_002() {
		Logger.start(true,"不输入pid失败");
		try {
			seckillproductstockchangeCall.setData(seckillproductstockchangeBean);
			seckillproductstockchangeCall.callService();			
			Logger.verifyEquals("ok", seckillproductstockchangeCall.getReturnData(), "验证返回");
			//验证日志
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	@Ignore
	public void Tc_SecKillProductStockChange_003() {
		Logger.start(true,"输入不存在pid失败");
		try {
			seckillproductstockchangeBean.setProductId(TestDataManager.getRandomUUID().toString());
			seckillproductstockchangeCall.setData(seckillproductstockchangeBean);
			seckillproductstockchangeCall.callService();			
			Logger.verifyEquals("ok", seckillproductstockchangeCall.getReturnData(), "验证返回");
			//验证日志
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	@Ignore
	public void Tc_SecKillProductStockChange_004() {
		Logger.start(true,"输入pid不是在售商品失败");
		try {
			List<Userinfo> list =new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =ProductManagerCallServiceV2.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.RemoveLiveProduct(createlive.getId(), pid);
			seckillproductstockchangeBean.setProductId(pid);
			seckillproductstockchangeCall.setData(seckillproductstockchangeBean);
			seckillproductstockchangeCall.callService();			
			Logger.verifyEquals("ok", seckillproductstockchangeCall.getReturnData(), "验证返回");		
			//验证日志
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	@Ignore
	public void Tc_SecKillProductStockChange_005() {
		Logger.start(true,"输入pid是已结束直播商品,失败");
		try {
			List<Userinfo> list =new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =ProductManagerCallServiceV2.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.SysCloseLive(createlive.getId());
			seckillproductstockchangeBean.setProductId(pid);
			seckillproductstockchangeCall.setData(seckillproductstockchangeBean);
			seckillproductstockchangeCall.callService();			
			Logger.verifyEquals("ok", seckillproductstockchangeCall.getReturnData(), "验证返回");		
			//验证日志
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}